package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.ParseException;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListObject extends ListActivity {
	ArrayList<Float> r = new ArrayList<Float>();

	static float latitude;
	static float longitude;
	static JSONObject obj;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, SearchObject.result));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list_object, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView,
	 * android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		JSONObject json_data = null;
		super.onListItemClick(l, v, position, id);
		String name = getListView().getItemAtPosition(position).toString();
		for (int i = 0; i < SearchObject.jArr.length(); i++) {
			try {
				json_data = SearchObject.jArr.getJSONObject(i);
				String name2 = json_data.getString("name");
				if (name.equals(name2)) {
					obj = json_data;
					r = getLocation();
					latitude = r.get(0);
					longitude = r.get(1);
					startActivity(new Intent(this, Object.class));
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// Log.d("A", json_data.getString("name"));

		}
	}

	private ArrayList<Float> getLocation() {
		ArrayList<Float> r = new ArrayList<Float>();
		int id_Location = 0;
		try {
			id_Location = ListObject.obj.getInt("id_Location");

			JSONObject json_data;
			
			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("id_Location", Integer
					.toString(id_Location)));
			String result = null;

			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/LocationController/getLocation", postParameters);
			JSONArray jArray = new JSONArray(result);
			for (int i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				// Log.d("A", json_data.getString("name"));
				r.add(Float.parseFloat(json_data.getString("x(point)")));
				r.add(Float.parseFloat(json_data.getString("y(point)")));
			}
			// return r;
		} catch (JSONException e1) {
			// Toast.makeText(((ContextWrapper) activity).getBaseContext(),
			// e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (ParseException e1) {
			// Toast.makeText(((ContextWrapper) activity).getBaseContext(),
			// e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return r;

	}

}
